/**
 * 项目列表功能
 *
 * @type {project_list}
 */
var project_list = function() {

      //项目列表操作栏图标操作
    var operation_icon=[
        {id:"edit",icon_url:"/edit.gif",description:language.common.update},
        {id:"remove",icon_url:"/remove.gif",description:language.common.remove}
    ];
	var grid_id = 'project_list';
	return {
		/**
		 * 显示列表
		 */
		list : function() {
			cdms.common.jart_grid(grid_id, {
				sortField : 'created',
				url : "list.json",
				idFields : 'id',
				gridline : 'horizontal',
                showRadio:'true',
                showCheckBox:'false',
				columns : [ [ {
					title : language.project.column_1,
					field : "code",
					headAlign : 'left',
					width : "100px",
					sortfield : "code"
				}, {
					title : language.project.column_2,
					field : "name",
					headAlign : 'left',
					width : "150px",
					formatter:project_list.name_format
				}, {
					title : language.project.column_3,
					field : "url",
					headAlign : 'left',
					 formatter:project_list.url_format
				}, {
					title : language.project.column_4,
					field : "lead",
					headAlign : 'left',
					width : "130px"
				},
				// { title:language.project.column_5, field:"team"
				// ,headAlign:'left',sortfield: "team"},
				{
					title : language.project.column_6,
					field : "created",
					headAlign : 'left',
					width : "130px",
					sortfield : "created"
				}, {
					title : language.common.operation,
					field : "operation",
					align : 'center',
					width : "120px",
					formatter : project_list.operationFormatter
				} ] ],
               // 选中触发事件
                onRadio  : function () {
                    project_list.toolbar_disabled_check();
                },
                //更新列表触发事件
                onUpdate  : function () {
                    project_list.toolbar_disabled_check();
                    }
			});
		},
        /**
         * checkRows  disabled操作函数
         */
        toolbar_disabled_check: function () {

            var rows = $('#' + grid_id).grid("radioedRow");
            var tools = $('#project_toolbar').toolbar();
             if(rows!=null){
             	tools.ritems(0)==undefined?null:tools.ritems(0).attr("disabled",false);
                $("#store_pid_0").attr("value",rows.attr("data").id)
             }
            else{
				tools.ritems(0)==undefined?null:tools.ritems(0).attr("disabled",true);
      		} 
       },

		/**
		 * 跳转到新增
		 */
		add : function() {
			window.location.href = "edit/0";
		},

        /**
         * 导入项目成员
         */
        importTeam      : function () {
            $("#store_pid").attr("value",$("#store_pid_0").val());

            $('#upload_file').layer({title: language.account.import_exl_data, target: "top"}).open();
            cdms.common.form_submit('import_form', {}, function (data) {
                if (data.type) {
                    $.alert(data.msg, language.note.alert_title);
                    cdms.common.show_ajax_msg(data.msg);
                    project_list.closeImport();
                    project_list.reload();
                } else {
                    $.alert(data.msg, language.note.alert_title);
                }
            });
            $('#upload_file_cancel').click(function () {
                project_list.closeImport();
            })

        },
        closeImport : function () {
            $('#upload_file').layer().close();
        },
		/**
		 * 删除操作
		 */
		remove : function(id) {
			var ids_post = null;
			if (id == 0) {
				var rows = $('#' + grid_id).grid("checkedRows");
				if (rows.length == 0) {
					$.alert(language.issue.not_checked,
							language.note.alert_title);
					return undefined;
				}
				ids_post = cdms.common.grid_select_ids(grid_id);
			} else {
				ids_post = id;
			}
			cdms.common.confirm(language.project.remove_tip_msg, function() {
				cdms.common.ajax_post('delete', {
					ids : ids_post
				}, function(json) {
					var data =  eval('(' + json + ')');
					if(data.type==0){
						$.alert(data.msg,language.alert.title);
					}
					$('#' + grid_id).grid().loadData();
				});
			});
		},

		/**
		 * format the hyper link of operation
         *
		 * @param item
		 * @param index
		 * @return {*}
		 */
		operationFormatter : function(item, index) {
			var template="";
			if (role == '"Admin"') {
                template = '<a href="edit/{0}"><img src="' +js_param.image_url +operation_icon[0].icon_url+'"title="'+operation_icon[0].description+'"height="16" width="16" border="0"/></a>' +
                    ' &nbsp;&nbsp;<a href="#" onclick="project_list.remove({0})"><img src="' + js_param.image_url +operation_icon[1].icon_url+'"title="'+operation_icon[1].description+'"height="16" width="16" border="0"/></a>';
			}
			if (role == '"Leader"') {
                template = '<a href="edit/{0}"><img src="' + js_param.image_url +operation_icon[0].icon_url+'"title="'+operation_icon[0].description+'"height="16" width="16" border="0"/></a>';
			}
			if(role=='"User"'){
				template="";
			}
			return template.format(item.id, language.common.update,
					language.common.remove);
		},
		/**
		 * Project search operation
		 */
		fuzzy_search : function() {
			var params = {
				'search' : $('#search').val()
			};
			$('#' + grid_id).grid({
				url : "fuzzy_search",
				'queryParams' : params,
				'pageindex' : 1
			});

			$('#' + grid_id).grid().loadData();
		},
		
		url_format:function(item) {
			var template = '<a href="'+item.url+'">'+item.url+'</a>';
			return template;
		},
		name_format:function(item){
			var template = '<a href="project/p_dashboard/'+item.code+'">'+item.name+'</a>';
			return template;
		},
		/**
		 * 刷新表格
		 */
		reload : function() {
			$('#' + grid_id).grid().refresh();
		}
	}
}();

$(document).ready(function() {
	project_list.list();
});

